home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / PixMap2PixPat2ppat / PixMap2PixPat2ppat.c next >
Encoding:
C/C++ Source or Header  |  1992-10-19  |  6.1 KB  |  267 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    PixMap2PixPat2ppat                                        */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows how to convert a 'icl8' image to a    */
  6. /*                    PixMap image and then to a PixPat and then finally to    */
  7. /*                    a 'ppat' resource.    In this example, the 'ppat'            */
  8. /*                    resource is saved-off into a resource file.                */
  9. /*                                                                            */
  10. /*    Files:            PixMap2PixPat2ppat.π                                    */
  11. /*                    PixMap2PixPat2ppat.c                                    */
  12. /*                    PixMap2PixPat2ppat.π.rsrc                                */
  13. /*                                                                            */
  14. /*    Programmer:        Edgar Lee                                                */
  15. /*    Organization:    Apple Computer, Inc.                                    */
  16. /*    Department:        Developer Technical Support, DTS                        */
  17. /*    Language:        C (Think C version 5.0.2)                                */
  18. /*    Date Created:    09-02-92                                                */
  19. /*                                                                            */
  20. /****************************************************************************/
  21.  
  22. /* Constant Declarations */
  23.  
  24. #define    WWIDTH        320
  25. #define    WHEIGHT        160
  26.  
  27. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  28. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  29.  
  30. /* Global Variable Definitions */
  31.  
  32. WindowPtr    gWindow;
  33.  
  34. void initMac();
  35. void createWindow();
  36. void doEventLoop();
  37. void drawWindow();
  38.  
  39. void createPixMap();
  40.  
  41. void PixMap2PixPat();
  42. void PixPat2ppat();
  43. void ppat2file();
  44.  
  45. main()
  46. {
  47.     PixMapHandle    pixmap;
  48.     PixPatHandle    pixpat;
  49.     
  50.     initMac();
  51.     
  52.     pixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
  53.     pixpat = NewPixPat();
  54.     
  55.     createWindow();
  56.     
  57.     createPixMap( pixmap );
  58.     PixMap2PixPat( pixmap, pixpat );
  59.     PixPat2ppat( pixpat );
  60.     
  61.     doEventLoop( pixpat );
  62. }
  63.  
  64. void initMac()
  65. {
  66.     MaxApplZone();
  67.     
  68.     InitGraf( &thePort );
  69.     InitFonts();
  70.     InitWindows();
  71.     InitMenus();
  72.     TEInit();
  73.     InitDialogs( nil );
  74.     InitCursor();
  75.     FlushEvents( 0, everyEvent );
  76. }
  77.  
  78. void createWindow()
  79. {
  80.     Rect rect;
  81.     
  82.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  83.     
  84.     gWindow = NewCWindow( 0L, &rect, "\pPixMap2PixPat2ppat", true, noGrowDocProc,
  85.                             (WindowPtr)-1L, true, 0L );
  86.     SetPort( gWindow );
  87. }
  88.  
  89. void createPixMap( pixmap )
  90. PixMapHandle    pixmap;
  91. {
  92.     Handle        iclHandle;
  93.     char        depth;
  94.     Rect        rect;
  95.         
  96.     SetRect( &rect, 0, 0, 32, 32 );
  97.     depth = 8;
  98.     
  99.     /* Create a pixmap from the icl8 pixel image. */
  100.     
  101.     iclHandle = GetResource( 'icl8', 129 );
  102.     
  103.     HLock( iclHandle );
  104.     HNoPurge( iclHandle );
  105.         
  106.     (**pixmap).baseAddr = *iclHandle;
  107.     (**pixmap).rowBytes = (((rect.right - rect.left) * depth) / 8) | 0x8000;
  108.     (**pixmap).bounds = rect;
  109.     (**pixmap).pmVersion = 0;
  110.     (**pixmap).packType = 0;
  111.     (**pixmap).packSize = 0;
  112.     (**pixmap).hRes = 0x00480000;
  113.     (**pixmap).vRes = 0x00480000;
  114.     (**pixmap).pixelSize = depth;
  115.     (**pixmap).planeBytes = 0;
  116.     (**pixmap).pmReserved = 0;
  117.     (**pixmap).pixelType = 0;
  118.     (**pixmap).cmpCount = 1;
  119.     (**pixmap).cmpSize = depth;
  120.     (**pixmap).pmTable = GetCTable( depth );
  121. }
  122.  
  123. void PixMap2PixPat( pixmap, pixpat )
  124. PixMapHandle    pixmap;
  125. PixPatHandle    pixpat;
  126. {
  127.     Handle      image;
  128.     long        imageSize;
  129.     
  130.     (**pixpat).patMap = pixmap;
  131.     
  132.     imageSize = (**(**pixpat).patMap).rowBytes *
  133.                 ((**(**pixpat).patMap).bounds.bottom -
  134.                 (**(**pixpat).patMap).bounds.top);
  135.                 
  136.     PtrToHand( (**pixmap).baseAddr, &image, imageSize );
  137.     (**pixpat).patData = image;
  138. }
  139.  
  140. void PixPat2ppat( pixpat )
  141. PixPatHandle    pixpat;
  142. {
  143.     Handle            ppat;
  144.     PixPatHandle    pixpatCopy;
  145.     long            pixmapSize, pixpatSize, imageSize, ctableSize;
  146.     
  147.     pixpatCopy = NewPixPat();
  148.     CopyPixPat( pixpat, pixpatCopy );
  149.     
  150.     pixmapSize    = sizeof( PixMap );
  151.     pixpatSize    = sizeof( PixPat);
  152.     imageSize    = (**(**pixpatCopy).patMap).rowBytes *
  153.                 ((**(**pixpatCopy).patMap).bounds.bottom -
  154.                 (**(**pixpatCopy).patMap).bounds.top);
  155.     ctableSize    = sizeof( ColorTable ) +
  156.                 (**(**(**pixpatCopy).patMap).pmTable).ctSize *
  157.                 sizeof( ColorSpec );
  158.     
  159.     /********************************************/
  160.     /* Allocate memory for the 'ppat' resource. */
  161.     /********************************************/
  162.     
  163.     ppat = NewHandle( pixpatSize + pixmapSize + imageSize + ctableSize );
  164.     
  165.     /**********************/
  166.     /* Fill the resource. */
  167.     /**********************/
  168.     
  169.     BlockMove( *(**(**pixpatCopy).patMap).pmTable,
  170.                 *ppat + pixpatSize + pixmapSize + imageSize, ctableSize );
  171.     (**(**pixpatCopy).patMap).pmTable = (CTabHandle)(pixpatSize + pixmapSize + imageSize);
  172.     
  173.     BlockMove( *(**pixpatCopy).patData, *ppat + sizeof( PixPat ) + sizeof( PixMap), imageSize );
  174.     (**pixpatCopy).patData = (Handle)(pixpatSize + pixmapSize);
  175.     
  176.     BlockMove( *(**pixpatCopy).patMap, *ppat + sizeof( PixPat ), pixmapSize );
  177.     (**pixpatCopy).patMap = (PixMapHandle)pixpatSize;
  178.     
  179.     BlockMove( *pixpatCopy, *ppat, pixpatSize );
  180.     
  181.     /************************************************/
  182.     /* Finally, save the 'ppat' resource to a file. */
  183.     /************************************************/
  184.  
  185.     ppat2file( ppat, "\pppat file" );
  186. }
  187.  
  188. void ppat2file( ppat, fname )
  189. Handle    ppat;
  190. Str255    fname;
  191. {
  192.     int        refNum;
  193.     
  194.     CreateResFile( fname );
  195.     
  196.     if (ResError() == noErr)
  197.     {
  198.         refNum = OpenResFile( fname );
  199.         
  200.         if (refNum != -1)
  201.         {
  202.             AddResource( ppat, 'ppat', UniqueID( 'ppat' ), "\pMy ppat" );
  203.             CloseResFile( refNum );
  204.         }
  205.     }
  206. }
  207.  
  208. void drawWindow( pixpat )
  209. PixPatHandle    pixpat;
  210. {
  211.     FillCRect( &(*gWindow).portRect, pixpat );
  212.     
  213.     TextFont( times );
  214.     TextSize( 72 );
  215.     TextMode( srcOr );
  216.     
  217.     ForeColor( blackColor );
  218.     MoveTo( 80, 100 );
  219.     DrawString( "\pDone." );
  220.     
  221.     ForeColor( yellowColor );
  222.     MoveTo( 75, 95 );
  223.     DrawString( "\pDone." );
  224. }
  225.  
  226. void doEventLoop( pixpat )
  227. PixPatHandle    pixpat;
  228. {
  229.     EventRecord event;
  230.     WindowPtr   window;
  231.     short       clickArea;
  232.     Rect        screenRect;
  233.  
  234.     for (;;)
  235.     {
  236.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  237.         {
  238.             if (event.what == mouseDown)
  239.             {
  240.                 clickArea = FindWindow( event.where, &window );
  241.                 
  242.                 if (clickArea == inDrag)
  243.                 {
  244.                     screenRect = (**GetGrayRgn()).rgnBBox;
  245.                     DragWindow( window, event.where, &screenRect );
  246.                 }
  247.                 else if (clickArea == inContent)
  248.                 {
  249.                     if (window != FrontWindow())
  250.                         SelectWindow( window );
  251.                 }
  252.                 else if (clickArea == inGoAway)
  253.                     if (TrackGoAway( window, event.where ))
  254.                         return;
  255.             }
  256.             else if (event.what == updateEvt)
  257.             {
  258.                 window = (WindowPtr)event.message;    
  259.                 SetPort( window );
  260.                 
  261.                 BeginUpdate( window );
  262.                 drawWindow( pixpat );
  263.                 EndUpdate( window );
  264.             }
  265.         }
  266.     }
  267. }